package nicg.st.controller;

import java.io.File;
import java.util.List;

//import nicg.st.Communicate;
import nicg.st.entity.IPParseBean;
import nicg.st.service.XmlService;
import nicg.st.util.ErrorCode;
import nicg.st.util.ResponseUtil;
import nicg.st.util.XMLUtil;
import nicg.st.util.XmlParseIp;

import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @author 
 * @Description
 * @date 2019/11/3 23:31
 */
@RestController
@RequestMapping("/api/xml")
public class XmlController {
    @Autowired
    XmlService xmlService;
    
//    XmlService xmlService = new XmlService();
//    Communicate communicate = Communicate.getSingleton();


    @RequestMapping(value = "/xmlImport")
    public String XmlImport(@RequestParam("file") MultipartFile file) {
        Document document = XMLUtil.getDocumentByFile(file);
        if (null != document) {
            Element root = document.getRootElement();
//           XMLUtil.listNodes(root, 0);
            xmlService.listNodes(root);
        }
        return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL , "successful" ,"1");
    }

    @RequestMapping(value = "/xmlExport")
    public String XmlExport(HttpServletResponse response, int id) {
        return xmlService.downloadExcel(response, id);
    }
    
    @RequestMapping(value = "/wordExport")
    public void wordExport(HttpServletResponse response, int id) {
        xmlService.downloadWord(response, id);
    }
    
    @RequestMapping(value = "/sendFile", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sendFile(int id){
    	xmlService.createDocumentToNative(id);
    	List<IPParseBean> ips = XmlParseIp.getIPParseBeans(new File("./config/ip.xml"));
    	xmlService.sendFile(ips.get(0).getIP());
    	return ResponseUtil.getResponseString(ErrorCode.SUCCESSFUL , "successful" ,"1");
    }
    
}
